package com.smalls.newvideotwo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.SeekBar;

import java.util.Locale;

@SuppressLint("AppCompatCustomView")
public class NumberSeekBar extends SeekBar {
    private Bitmap bm;
    private int c = 3;
    private int d = 2;
    private int imagepaddingleft;
    private int imagepaddingtop;
    private int increment = 1;
    private boolean isMysetPadding = true;
    private boolean isPause;
    private boolean isShow = false;
    private int mDuration = 1;
    private float mImgHei;
    private float mImgWidth;
    private Paint mPaint;
    private String mText;
    private float mTextWidth;
    private int maxMultiple = 10;
    private int oldPaddingBottom;
    private int oldPaddingLeft;
    private int oldPaddingRight;
    private int oldPaddingTop;
    private Resources res;
    private int textpaddingleft;
    private int textpaddingtop;
    private int textsize = 13;

    public NumberSeekBar(Context context) {
        super(context);
        init();
    }

    public NumberSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NumberSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    public void setPadding(int left, int top, int right, int bottom) {
        if (this.isMysetPadding) {
            super.setPadding(left, top, right, bottom);
        }
    }

    private void init() {
        this.res = getResources();
        initBitmap();
        initDraw();
        setPadding();
    }

    private void initDraw() {
        this.mPaint = new Paint(1);
        this.mPaint.setTypeface(Typeface.DEFAULT);
        this.mPaint.setTextSize((float) this.textsize);
        this.mPaint.setColor(ViewCompat.MEASURED_STATE_MASK);
    }

    private void initBitmap() {
        this.bm = null;
        if (this.bm != null) {
            this.mImgWidth = (float) this.bm.getWidth();
            this.mImgHei = (float) this.bm.getHeight();
            return;
        }
        this.mImgWidth = 60.0f;
        this.mImgHei = 30.0f;
    }

    private void setIncrement() {
        this.c--;
        if (this.c <= 0) {
            if (this.d <= this.maxMultiple && this.increment != 0) {
                setKeyProgressIncrement(this.d * this.increment);
                this.d++;
            }
            this.c = 3;
        }
    }

    public void setMax(int max) {
        super.setMax(max);
        this.increment = max / 500;
    }

    public void setPause(boolean isPause) {
        this.isPause = isPause;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
            case KeyEvent.KEYCODE_DPAD_RIGHT /*22*/:
                if (!this.isPause) {
                    setIncrement();
                    break;
                }
                return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT /*21*/:
            case KeyEvent.KEYCODE_DPAD_RIGHT /*22*/:
                this.c = 3;
                this.d = 2;
                setKeyProgressIncrement(this.increment);
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    protected synchronized void onDraw(Canvas canvas) {
        try {
            super.onDraw(canvas);
            if (this.isShow) {
                this.mText = showTime((long) ((((float) getProgress()) / 10000.0f) * (((float) this.mDuration) / 1000.0f)));
                this.mTextWidth = this.mPaint.measureText(this.mText);
                Rect bounds = getProgressDrawable().getBounds();
                float xImg = (float) ((((bounds.width() * getProgress()) / getMax()) + this.imagepaddingleft) + this.oldPaddingLeft);
                canvas.drawText(this.mText, (((((float) ((bounds.width() * getProgress()) / getMax())) + (this.mImgWidth / 2.0f)) - (this.mTextWidth / 2.0f)) + ((float) this.textpaddingleft)) + ((float) this.oldPaddingLeft), ((((float) this.textpaddingtop) + ((float) (this.imagepaddingtop + this.oldPaddingTop))) + (this.mImgHei / 2.0f)) + (getTextHei() / 4.0f), this.mPaint);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setPadding() {
        int top = getBitmapHeigh() + this.oldPaddingTop;
        int left = (getBitmapWidth() / 2) + this.oldPaddingLeft;
        int right = (getBitmapWidth() / 2) + this.oldPaddingRight;
        int bottom = this.oldPaddingBottom;
        this.isMysetPadding = true;
        setPadding(left, top, right, bottom);
        this.isMysetPadding = false;
    }

    public void setBitmap(int resid) {
        this.bm = null;
        if (this.bm != null) {
            this.mImgWidth = (float) this.bm.getWidth();
            this.mImgHei = (float) this.bm.getHeight();
        } else {
            this.mImgWidth = 60.0f;
            this.mImgHei = 30.0f;
        }
        setPadding();
    }

    public String showTime(long time) {
        int minute = ((int) time) / 60;
        int hour = minute / 60;
        int second = ((int) time) % 60;
        minute %= 60;
        return String.format(Locale.US, "%02d:%02d:%02d", new Object[]{Integer.valueOf(hour), Integer.valueOf(minute), Integer.valueOf(second)});
    }

    public void setMyPadding(int left, int top, int right, int bottom) {
        this.oldPaddingTop = top;
        this.oldPaddingLeft = left;
        this.oldPaddingRight = right;
        this.oldPaddingBottom = bottom;
        this.isMysetPadding = true;
        setPadding((getBitmapWidth() / 2) + left, getBitmapHeigh() + top, (getBitmapWidth() / 2) + right, bottom);
        this.isMysetPadding = false;
    }

    public void setTextSize(int textsize) {
        this.textsize = textsize;
        this.mPaint.setTextSize((float) textsize);
    }

    public void setTextVisiable(boolean isShow) {
        this.isShow = isShow;
    }

    public void setDuration(int mDuration) {
        if (mDuration > 0) {
            this.mDuration = mDuration;
        } else {
            this.mDuration = 1;
        }
        if (mDuration >= 3600000) {
            this.maxMultiple = 30;
            this.increment = getMax() / 500;
        } else if (mDuration >= 1800000) {
            this.maxMultiple = 20;
            this.increment = getMax() / 500;
        } else if (mDuration >= 600000) {
            this.maxMultiple = 10;
            this.increment = getMax() / 100;
        } else {
            this.maxMultiple = 3;
            this.increment = getMax() / 50;
        }
    }

    public void setTextColor(int color) {
        this.mPaint.setColor(color);
    }

    public void setTextPadding(int top, int left) {
        this.textpaddingleft = left;
        this.textpaddingtop = top;
    }

    public void setImagePadding(int top, int left) {
        this.imagepaddingleft = left;
        this.imagepaddingtop = top;
    }

    private int getBitmapWidth() {
        return (int) Math.ceil((double) this.mImgWidth);
    }

    private int getBitmapHeigh() {
        return (int) Math.ceil((double) this.mImgHei);
    }

    private float getTextHei() {
        FontMetrics fm = this.mPaint.getFontMetrics();
        return ((float) Math.ceil((double) (fm.descent - fm.top))) + 2.0f;
    }

    public int getTextpaddingleft() {
        return this.textpaddingleft;
    }

    public int getTextpaddingtop() {
        return this.textpaddingtop;
    }

    public int getImagepaddingleft() {
        return this.imagepaddingleft;
    }

    public int getImagepaddingtop() {
        return this.imagepaddingtop;
    }

    public int getTextsize() {
        return this.textsize;
    }
}
